package one.stand.controller;

import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.base.dto.SunCodeCardRequest;
import com.jmp.base.dto.SunCodeCardResponse;
import com.jmp.feign.BannerFeign;
import com.jmp.feign.SunCodeFeign;
import com.jmp.service.api.admin.BannerApi;
import com.jmp.service.api.admin.dto.BannerListDto;
import com.jmp.service.api.admin.dto.BannerListVo;
import io.swagger.annotations.Api;
import one.stand.dto.ConvertUtils;
import one.stand.dto.IndexResponse;
import one.stand.dto.UserDto;
import one.stand.model.*;
import one.stand.service.BusinessCardBaseService;
import one.stand.service.CompanySelectService;
import one.stand.service.CompanyShowroomService;
import one.stand.service.user.UserAdminService;
import one.stand.util.AssertUtil;
import one.stand.util.RequestContextHolder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * @author cjr
 */
@Api(description = "首页")
@RestController
@RequestMapping("/index")
public class IndexController {
    @Autowired
    private SunCodeFeign sunCodeFeign;
    @Autowired
    private BannerFeign bannerFeign;

    @Autowired
    private BusinessCardBaseService businessCardBaseService;
    @Autowired
    private CompanyShowroomService companyShowroomService;
    @Autowired
    private UserAdminService userAdminService;
    @Autowired
    private CompanySelectService companySelectService;

    @PostMapping("")
    @SecurityAuthCheck(company = true)
    public ResultModel<IndexResponse> index() {
        final Integer cardId = RequestContextHolder.getRequestContext().getUser().getCardId();

        ResultModel<SunCodeCardResponse> sunCodeCardResponseResultModel = sunCodeFeign.card(SunCodeCardRequest.builder().cardId(cardId).build());
        AssertUtil.isTrue(sunCodeCardResponseResultModel);
        String wxCode = sunCodeCardResponseResultModel.getData().getCodeUrl();

        BusinessCard businessCardModel = businessCardBaseService.getCustomer(cardId);

        UserDto user = userAdminService.get(businessCardModel.getUserId());

        //banner
        BannerListDto bannerListDto = BannerListDto.builder()
                .companyId(businessCardModel.getCompanyId())
                .bannerType(BannerApi.BANNER_TYPE_SHOWROOM)
                .refId(null)
                .build();
        bannerListDto.setAll();
        ResultModel<BannerListVo> bannerListVoResultModel = bannerFeign.list(bannerListDto);
        AssertUtil.isTrue(bannerListVoResultModel);
        List<IndexResponse.Banner> banners = ConvertUtils.list(bannerListVoResultModel.getData().getBannerPage().getList(), companyBanner -> IndexResponse.Banner.builder()
                .source(companyBanner.getSource())
                .type(companyBanner.getType())
                .event(String.valueOf(companyBanner.getEvent()))
                .id(companyBanner.getEventId())
                .build());

        IndexResponse loginResponse = IndexResponse.builder()
                .name(user.getName())
                .icon(user.getUserImg())
                .wxCode(wxCode)
                .position(businessCardModel.getPosition())
                .banners(banners)
                .build();

        CompanyShowroom companyShowroom = companyShowroomService.getByCompanyId(businessCardModel.getCompanyId());
        if (Objects.nonNull(companyShowroom)) {
            loginResponse.setVideoType(0);
            if (CompanyShowroomService.SHOW_VIDEO_ON == companyShowroom.getShowVideo()
                    && StringUtils.isNoneBlank(companyShowroom.getVideo())) {
                loginResponse.setVideoUrl(companyShowroom.getVideo());
            } else if (CompanyShowroomService.SHOW_VIDEO_OFF == companyShowroom.getShowVideo()
                    && StringUtils.isNoneBlank(companyShowroom.getVrUrl())) {
                loginResponse.setVideoType(1);
                loginResponse.setVideoUrl(companyShowroom.getVrUrl());
            }
            loginResponse.setVrUrl(companyShowroom.getVrUrl());
        }

        if (StringUtils.isBlank(loginResponse.getVideoUrl()) || StringUtils.isBlank(loginResponse.getVrUrl())) {
            String videUrl = LoginController.DEFAULT_VIDEO;
            String vrUrl = StringUtils.EMPTY;
            CompanySelectAgree companySelectAgree = companySelectService.getByCompanyId(businessCardModel.getCompanyId());
            if (Objects.nonNull(companySelectAgree)) {//存在优选
                companyShowroom = companyShowroomService.getByCompanyId(companySelectAgree.getSelectCompanyId());
                if (Objects.nonNull(companyShowroom)) {//存在展厅配置
                    videUrl = companyShowroom.getVideo();
                    vrUrl = companyShowroom.getVrUrl();
                }
            }
            if (StringUtils.isBlank(loginResponse.getVideoUrl())) {
                loginResponse.setVideoType(0);
                loginResponse.setVideoUrl(videUrl);
            }
            if (StringUtils.isBlank(loginResponse.getVrUrl())) {
                loginResponse.setVrUrl(vrUrl);
            }
        }

        return ResultModel.success(loginResponse);
    }
}
